﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.ComponentModel;

namespace IKYSWeb.UserControls
{
    public partial class Ascx_Sayi : System.Web.UI.UserControl
    {
        public enum SayiTipi
        {
            Integer = 0,
            Double = 1
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (ZeroWhenEmpty)
            {
                if (String.IsNullOrEmpty(txtSayi.Text))
                {
                    if (DataType == SayiTipi.Double)
                        txtSayi.Text = "0,0";
                    else
                        txtSayi.Text = "0";
                }
            }
        }

        public double OndalikSayi
        {
            get
            {
                if (txtSayi.Text != "")
                    return Convert.ToDouble(txtSayi.Text);
                else
                    return 0;
            }
            set
            {
                txtSayi.Text = (value != 0) ? value.ToString() : "";
            }
        }

        public int Sayi
        {
            get
            {
                if (txtSayi.Text != "")
                    return Convert.ToInt32(txtSayi.Text);
                else
                    return 0;
            }
            set
            {
                txtSayi.Text = (value != 0) ? value.ToString() : "";
            }
        }

        public void Clear()
        {
            txtSayi.Text = "";
        }

        public int MaxLength
        {
            get { return txtSayi.MaxLength; }
            set { txtSayi.MaxLength = value; }
        }

        public bool ReadOnly
        {
            set { txtSayi.ReadOnly = value; }
        }

        public bool AllowEmpty
        {
            set { rfvSayi.Enabled = !value; }
        }

        public bool ZeroWhenEmpty
        {
            get
            {
                try
                {
                    return (bool)ViewState["ZeroWhenEmpty"];
                }
                catch (Exception)
                {
                    ZeroWhenEmpty = false;
                }
                return ZeroWhenEmpty;
            }
            set { ViewState["ZeroWhenEmpty"] = value; }
        }
        public SayiTipi DataType
        {
            set
            {
                if (value == SayiTipi.Integer)
                {
                    cvSayi.Type = ValidationDataType.Integer;
                    txtSayi.CssClass = "int";
                }
                else
                {
                    cvSayi.Type = ValidationDataType.Double;
                    txtSayi.CssClass = "float";
                }
            }
            get
            {
                return (cvSayi.Type == ValidationDataType.Integer) ? SayiTipi.Integer : SayiTipi.Double;
            }
        }

        public string ValidationGroup
        {
            set { rfvSayi.ValidationGroup = value; }
        }

        public string HataMesaji
        {
            set { rfvSayi.ErrorMessage = value; }
        }

        public int Width
        {
            set { txtSayi.Width = value; }
        }
        public TextBox SayiTextBox
        {
            get
            {
                return txtSayi;
            }
        }

    }
}